package app

import (
	"github.com/kataras/iris"
)

type ResponseJson struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type Response struct {
	ctx iris.Context
}

func (this *Response) End(httpCode, code int, msg string, data interface{}) {

	req := ResponseJson{
		Code: code,
		Msg: msg,
		Data: data,
	}
	this.ctx.JSON(req)
	return
}

func (this *Response) EndSuccess(data interface{})  {
	this.End(200, 200, "ok", data)
}

func (this *Response) EndError(code int, msg string)  {
	if code == 0 {
		code = 500
	}
	this.End(200, code, msg, map[string]interface{}{})
}

func (this *Response) AutoEnd(response interface{}, err error)  {
	if err != nil {
		this.EndError(0, err.Error())
		return
	}
	this.EndSuccess(response)
}
